#!/bin/bash

### BEGIN INIT INFO
# Provides:          structr-ui
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start Structr.
# Description:       Start Structr UI (see http://structr.org).
### END INIT INFO

cd /usr/lib/structr/

# set max open files limits to recommended value
ulimit -Hn 50000
ulimit -Sn 40000

case "$1" in
  start)
        /usr/lib/structr/bin/start
        exit 0
        ;;
  status|info)
		PID=$(pgrep -P 1 -f org.structr.Server)
		if [[ ! -z $PID ]]; then
			echo "There may be a Structr instance running with pid $PID, exiting."
			exit 1
        else
            printf "%s\n" "Service not running"
            exit 1
        fi
  ;;
  restart)

        /usr/lib/structr/bin/stop && /usr/lib/structr/bin/start
        ;;
  stop)
        /usr/lib/structr/bin/stop
        ;;
  *)
        echo "Usage: structr {start|stop|status|restart}" >&2
        exit 3
        ;;
esac